<?php



namespace app\superadmin\controller;


use app\common\controller\SuperadminBase;
use think\Controller;

use think\Request;

use think\Session;

use think\Db;

use app\superadmin\model\Check as Model;

use app\superadmin\model\Service;

use app\superadmin\model\CheckService;




class Check extends SuperadminBase

{

    public function index()

    {   

        $classifylist = Db::table('yup_classify')->order('id')->select();

        $this->assign('classifylist', $classifylist);

        return $this->fetch();

    }



    public function gList(Request $request) {

         //获取每页显示的条数

        $limit= $request->param('limit');

        //获取当前页数

        $page= $request->param('page');


        $name = $request->param('name');


        $cid = $request->param('cid');

        $where = [];

        if (!empty($cid)) {
           
            $where['cid'] = $cid;
        }
      
       

        $list = Model::with('classifyinfo')->where('name', 'like', '%' . $name . '%')->where($where)->order('px,id desc')->paginate($limit,false,['page'=>$page])->toArray();    

        return json(['code'=>0,'msg'=>'','count'=>$list['total'],'data'=>$list['data']]);

    }




    public function add()
    {   

        if(\request()->isPost()){

            $data  = input('post.');

            $good = Model::create($data,true);

            $idarr = explode(',', $data['cIds']);

            for ($i=0; $i < count($idarr); $i++) { 
                
                CheckService::create(['cid'=>$good->id,'sid'=>$idarr[$i]]);
            }

            return json(['code'=>1, 'message'=>'新增成功！']);

        }else{

            $list = Service::order('px')->select();

            $classifylist = Db::table('yup_classify')->order('id')->select();

            $this->assign('list', $list);
            $this->assign('classifylist', $classifylist);

            return $this->fetch();

        }
        
    }


    // 编辑
    public function edit()
    {   

        if(\request()->isPost()){

            $data  = input('post.');

            CheckService::where('cid', $data['id'])->delete();

            $idarr = explode(',', $data['cIds']);

            for ($i=0; $i < count($idarr); $i++) { 
                
                CheckService::create(['cid'=>$data['id'],'sid'=>$idarr[$i]]);
            }

             Model::update($data, ['id'=>$data['id']], true);

             return json(['code'=>1, 'message'=>'修改成功']);


        }else{

            $id = $this->request->param('id');

            $info = Model::get(intval($id));

            $list = Service::order('px')->select();

            $classifylist = Db::table('yup_classify')->order('id')->select();

            $idarr = CheckService::where('cid',$id)->field('sid')->column('sid');

            $this->assign('list', $list);

            $this->assign('classifylist', $classifylist);

            $this->assign('info', $info);

            $this->assign('idarr', $idarr);

            return $this->fetch();

        }
        
    }



    //删除

    public function del() {

        $id = $this->request->param('id');

        Model::destroy($id);

        return 1;

    }



    public function editSxj()

    {
        $id    = $this->request->param('id');
        $is_xj = $this->request->param('is_xj');

        $info = Model::where('id', $id)->find();

        Model::where('id', $id)->update(['state' => $is_xj]);
        

        // return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
        return 1;

       
    }



    public function editpx()

    {
        $id    = $this->request->param('id');
        $px = $this->request->param('px');

        $info = Model::where('id', $id)->find();

        Model::where('id', $id)->update(['px' => $px]);
       

        //return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
        return 1;

       
    }





}

